package com.base.dataStructure.deque;


import org.springframework.scheduling.annotation.EnableAsync;

import java.util.Iterator;

/**
 * @Author: hdhao
 * @Create: 2023/4/1 - 22:54
 * @Version: v1.0
 */
//基于双向环形链表实现双端队列
public class LinkedListDeque<E> implements Deque<E>,Iterable<E>{
    int capacity;
    int size;
    Node<E> sentinel = new Node<>(null,null,null);

    public LinkedListDeque(int capacity) {
        this.capacity = capacity;
        sentinel.next = sentinel;
        sentinel.prev = sentinel;
    }

    /*
        a -> b
          <-
     */
    @Override
    public boolean offerFirst(E e) {
        if (isFull()){
            return false;
        }
        //找出要添加元素的上一个节点和下一个节点
        //因为是向队列头添加,所以,上一个节点是sentinel
        Node<E> a = sentinel;
        //下一个节点是sentinel.next
        Node<E> b = sentinel.next;
        //创建新节点
        Node<E> added = new Node<>(a, e, b);
        //更改指针指向
        a.next = added;
        b.prev = added;
        size++;
        return true;
    }

    @Override
    public boolean offerLast(E e) {
        if (isFull()){
            return false;
        }
        Node<E> last = this.sentinel;
        Node<E> prev = last.prev;
        Node<E> added = new Node<E>(prev,e,last);
        prev.next = added;
        last.prev = added;
        size++;
        return true;
    }
    //从头部删除
    @Override
    public E pollFirst() {
        if (isEmpty()){
            return null;
        }
        Node<E> removed = sentinel.next;
        Node<E> next = removed.next;
        sentinel.next = next;
        next.prev = sentinel;
        size--;
        return removed.value;
    }
    //从尾部删除
    @Override
    public E pollLast() {
        if (isEmpty()){
            return null;
        }
        Node<E> removed = sentinel.prev;
        Node<E> prev = removed.prev;
        sentinel.prev = prev;
        prev.next = sentinel;
        size--;
        return removed.value;
    }

    @Override
    public E peekFirst() {
        if (isEmpty()){
            return null;
        }
        return sentinel.next.value;
    }

    @Override
    public E peekLast() {
        if (isEmpty()){
            return null;
        }
        return sentinel.prev.value;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean isFull() {
        return size == capacity;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            Node<E> p = sentinel.next;
            @Override
            public boolean hasNext() {
                return p!=sentinel;
            }

            @Override
            public E next() {
                E value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    static class Node<E>{
        Node<E> prev;
        E value;
        Node<E> next;

        public Node(Node<E> prev, E value, Node<E> next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }
}
